<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>受控组件</title>

</head>
<body>
<div id="test"></div>
</body>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<!--将jsx转为js-->
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<!--babel  由babel转换jsx-->
<script type="text/babel">
    class Life extends React.Component {
        state = {
            opacity: 1
        }
        death = () => {
            ReactDOM.unmountComponentAtNode(document.getElementById('test'))
        }

        // 组件挂在完毕
        componentDidMount() {
            this.interVal = setInterval(() => {
                let {opacity} = this.state
                opacity -= 0.1
                if (opacity <= 0) opacity = 1
                this.setState({opacity})
            }, 200)
        }

        //卸载将要销毁
        componentWillUnmount() {
            clearInterval(this.interVal)
        }

        render() {
            return (
                <div>
                    <h2 style={{opacity: this.state.opacity}}>React学不会，怎么办？</h2>
                    <button onClick={this.death}>不活了</button>
                </div>
            )
        }
    }

    ReactDOM.render(<Life/>, document.getElementById('test'))
</script>
</html>